﻿using IDEAMS.core;
using IDEAMS.core.opm;
using IDEAMS.utils;
using log4net;
using System.IO;

namespace IDEAMS.model.device
{
    public class DeviceParam
    {
        private static ILog logger = LogManager.GetLogger(typeof(DeviceParam));

        public string type; // 设备型号
        public string serialNo; // 序列号
        public string version;  // 软硬件版本号
        public string ip;  // 本机IP地址
        public int port;  // 本机端口号
        public string mac;  // 本机MAC地址
        public string destIP;  // 目标IP地址
        public int destPort;  // 目标端口号
        public double temperature;  // 设备温度
        public int startFlag;  // 启动标志
        public int nChannel;  // 通道数
        public string netmask;  // 子网掩码
        public string gateway;  // 默认网关
        public byte reportFlag;  // 上报字段状态
        public string reportServerIP1;  // 上报服务IP1
        public int reportServerPort1;  // 上报服务端口1
        public string reportServerIP2;  // 上报服务IP2
        public int reportServerPort2;  // 上报服务端口2
        public string reportServerIP3;  // 上报服务IP3
        public int reportServerPort3;  // 上报服务端口3
        public uint timeInterval;  // 上报时间间隔（毫秒）

        public DeviceParam()
        {
            
        }

        public DeviceParam(DeviceParam deviceParam)
        {
            Copy(deviceParam);
        }

        public DeviceParam(DeviceParamViewModel deviceParam)
        {
            Copy(deviceParam);
        }

        public void Copy(DeviceParam deviceParam)
        {
            this.type = deviceParam.type;
            this.serialNo = deviceParam.serialNo;
            this.version = deviceParam.version;
            this.ip = deviceParam.ip;
            this.port = deviceParam.port;
            this.mac = deviceParam.mac;
            this.destIP = deviceParam.destIP;
            this.destPort = deviceParam.destPort;
            this.temperature = deviceParam.temperature;
            this.startFlag = deviceParam.startFlag;
            this.nChannel = deviceParam.nChannel;
            this.netmask = deviceParam.netmask;
            this.gateway = deviceParam.gateway;
            this.reportFlag = deviceParam.reportFlag;
            this.reportServerIP1 = deviceParam.reportServerIP1;
            this.reportServerPort1 = deviceParam.reportServerPort1;
            this.reportServerIP2 = deviceParam.reportServerIP2;
            this.reportServerPort2 = deviceParam.reportServerPort2;
            this.reportServerIP3 = deviceParam.reportServerIP3;
            this.reportServerPort3 = deviceParam.reportServerPort3;
            this.timeInterval = deviceParam.timeInterval;
        }

        public void Copy(DeviceParamViewModel deviceParam)
        {
            this.type = deviceParam.type;
            this.serialNo = deviceParam.serialNo;
            this.version = deviceParam.version;
            this.ip = deviceParam.ip;
            this.port = deviceParam.port;
            this.mac = deviceParam.mac;
            this.destIP = deviceParam.destIP;
            this.destPort = deviceParam.destPort;
            this.temperature = deviceParam.temperature;
            this.startFlag = deviceParam.startFlag;
            this.nChannel = deviceParam.nChannel;
            this.netmask = deviceParam.netmask;
            this.gateway = deviceParam.gateway;
            this.reportFlag = deviceParam.reportFlag;
            this.reportServerIP1 = deviceParam.reportServerIP1;
            this.reportServerPort1 = deviceParam.reportServerPort1;
            this.reportServerIP2 = deviceParam.reportServerIP2;
            this.reportServerPort2 = deviceParam.reportServerPort2;
            this.reportServerIP3 = deviceParam.reportServerIP3;
            this.reportServerPort3 = deviceParam.reportServerPort3;
            this.timeInterval = deviceParam.timeInterval;
        }

        public DeviceParam Clone()
        {
            return new DeviceParam(this);
        }

        public bool Save()
        {
            string dataFile = string.Format(TW_App.fileManager.deviceParamFile, serialNo);

            DeviceParam deviceParam = new DeviceParam(this);
            bool rn = XmlUtil.Save<DeviceParam>(dataFile, deviceParam);
            return rn;
        }

        public int Load()
        {
            int rn = 0;

            string dataFile = string.Format(TW_App.fileManager.deviceParamFile, serialNo);
            bool existsFlag = File.Exists(dataFile);
            if(!existsFlag)
            {
                return -1;
            }

            DeviceParam deviceParam = XmlUtil.Load<DeviceParam>(dataFile);
            if(deviceParam != null)
            {
                Copy(deviceParam);
                rn = 0;
            }
            else
            {
                logger.ErrorFormat("加载设备[{0}]信息失败",dataFile);
                return -1;
            }
            return rn;
        }

        public void FormResp(ReadDeviceBaseInfoResp info)
        {
            this.type = info.productModel;
            this.serialNo = info.seriesNumber;
            this.version = info.version;
            this.ip = DataConverter.ipAddressToString(info.deviceIP);
            this.port = info.devicePort;
            this.mac = DataConverter.macAddressToString(info.deviceMac);
            this.destIP = DataConverter.ipAddressToString(info.destIP);
            this.destPort = info.destPort;
            this.temperature = info.tempValue;
            this.startFlag = info.startFlag;
            this.nChannel = info.nchannel;
            this.netmask = DataConverter.ipAddressToString(info.netMask);
            this.gateway = DataConverter.ipAddressToString(info.gateWay);
            this.reportFlag = info.reserved1;
            this.reportServerIP1 = DataConverter.ipAddressToString(info.reportServerIP1);
            this.reportServerPort1 = info.reportServerPort1;
            this.reportServerIP2 = DataConverter.ipAddressToString(info.reportServerIP2);
            this.reportServerPort2 = info.reportServerPort2;
            this.reportServerIP3 = DataConverter.ipAddressToString(info.reportServerIP3);
            this.reportServerPort3 = info.reportServerPort3;
            this.timeInterval = info.timeInterval;
        }
    }
}
